import asyncio

from loguru import logger

from com.arcfox.middleware import async_mysql_middleware as db
from com.arcfox.util import util

subjects_mapping = {
    "物": "物理", "化": "化学",
    "生": "生物", "历": "历史",
    "地": "地理", "政": "政治",
    "物理": "物理", "化学": "化学",
    "生物": "生物", "历史": "历史",
    "地理": "地理", "政治": "政治",
    "思想政治": "政治", "史": "历史",
}


async def select_all_requirements():
    sql = "SELECT subject_requirement, plan_category FROM `tb_enrollment_plan` where subject_requirement " \
          "is not null and plan_category in ('历史类', '物理类', '综合') group by subject_requirement, plan_category"

    result = await db.query_all(sql)
    return result


def parse_requirements(record):
    subject_requirement = record[0]
    plan_category = record[1]
    if '综合' in plan_category:
        result = __parse_33_requirements(subject_requirement)
    else:
        result = __parse_312_requirements(plan_category, subject_requirement)
    result_str = '|'.join(result)
    logger.info(f"record: {record}, result: {result_str}")
    return result_str


def __process_subject_requirements(subject_requirement):
    if subject_requirement:
        subject_requirement = util.multi_replace(subject_requirement, ["必选", "首选", "再选", "(2选1)", "(2科必选)"], "")
        subject_requirement = subject_requirement.split("(")[0]
    return subject_requirement


def __parse_33_requirements(subject_requirement):
    if not subject_requirement:
        return None
    subject_requirement = __process_subject_requirements(subject_requirement)
    result_list = []
    if "/" in subject_requirement:
        data_list = subject_requirement.split("/")
        for item in data_list:
            result_list.append(subjects_mapping[item])
    elif "、" in subject_requirement:
        data_list = subject_requirement.split("、")
        data = ""
        for item in data_list:
            data += subjects_mapping[item] + ","
        result_list.append(data[0:-1])
    else:
        if subject_requirement in subjects_mapping:
            result_list.append(subjects_mapping[subject_requirement])
        else:
            data_list = list(subject_requirement)
            for item in data_list:
                if item in subjects_mapping:
                    result_list.append(subjects_mapping[item])
    return result_list


def __parse_312_requirements(plan_category, subject_requirement):
    plan_category = plan_category.replace("类", "")
    if not subject_requirement:
        return None
    subject_requirement = __process_subject_requirements(subject_requirement)
    subject_requirement = subject_requirement.split("，")[1]
    result_list = []
    if "/" in subject_requirement:
        data_list = subject_requirement.split("/")
        for item in data_list:
            result_list.append(plan_category + "," + subjects_mapping[item])
    elif "、" in subject_requirement:
        data_list = subject_requirement.split("、")
        data = ""
        for item in data_list:
            data += subjects_mapping[item] + ","
        result_list.append(plan_category + "," + data[0:-1])
    else:
        if subject_requirement in subjects_mapping:
            result_list.append(plan_category + "," + subjects_mapping[subject_requirement])
        else:
            result_list.append(plan_category)
    return result_list


async def start():
    records = await select_all_requirements()
    for record in records:
        result_str = parse_requirements(record)
        sql = f"update tb_enrollment_plan set subject_needs = '{result_str}' where subject_requirement = '{record[0]}'" \
              f" and plan_category = '{record[1]}'"
        logger.info(sql)
        await db.save_or_update(sql)


if __name__ == "__main__":
    asyncio.run(start())
